//
//  UIButton+SJExtesion.swift
//  dj-learn-Swift
//
//  Created by Sun Jae on 2022/4/24.
//  Copyright © 2022 S.J. All rights reserved.
//

import UIKit

class SJButton : UIButton {
    enum ImagePosition {
        case left
        case right
        case top
        case bottom
        
    }
    // 图片大小
    var imageSize: CGSize = .zero {
        didSet {
            self.layoutSubviews()
        }
    }
    // 中间间隔
    var midSpace: CGFloat = 0 {
        didSet {
            self.layoutSubviews()
        }
    }
    // 图片位置
    var imagePostion: ImagePosition = .left {
        didSet {
            self.layoutSubviews()
        }
    }
    
    // 重写layoutSubviews 布局子视图
    open override func layoutSubviews() {
        super.layoutSubviews()
        if imageSize.equalTo(.zero) {
            self.imageView?.sizeToFit()
        } else if let imageView = self.imageView {
            let x = imageView.frame.origin.x
            let y = imageView.frame.origin.y
            imageView.frame = CGRect(x: x, y: y, width: imageSize.width, height: imageSize.height)
        }
        self.titleLabel?.sizeToFit()
        
        switch self.imagePostion {
        case .left:
            self.layoutHorizontal(leftView: self.imageView, rightView: self.titleLabel)
            break
        case .right:
            self.layoutHorizontal(leftView: self.titleLabel, rightView: self.imageView)
            break
        case .top:
            self.layoutVertical(topView: self.imageView, bottomView: self.titleLabel)
            break
        case .bottom:
            self.layoutVertical(topView: self.titleLabel, bottomView: self.imageView)
            break
        }
    }
    
    // 水平方向布局
    private func layoutHorizontal(leftView: UIView?, rightView: UIView?) {
        guard let leftView = leftView, let rightView = rightView else { return }
        var leftViewFrame = leftView.frame
        var rightViewFrame = rightView.frame
        let totalWidth =  leftViewFrame.width + self.midSpace + rightViewFrame.width
        
        leftViewFrame.origin.x = 0.5 * (self.frame.width - totalWidth)
        leftViewFrame.origin.y = 0.5 * (self.frame.height - leftViewFrame.height)
        
        leftView.frame = leftViewFrame
        
        rightViewFrame.origin.x = leftViewFrame.maxX + self.midSpace
        rightViewFrame.origin.y = 0.5 * (self.frame.height - rightViewFrame.height)
        rightView.frame = rightViewFrame;
    }
    
    // 垂直方向布局
    private func layoutVertical(topView: UIView?, bottomView: UIView?) {
        guard let topView = topView, let bottomView = bottomView else { return }
        
        var topViewFrame = topView.frame
        var bottomViewFrame = bottomView.frame
        let totalHeight =  topViewFrame.height + self.midSpace + bottomViewFrame.height
        
        topViewFrame.origin.x = 0.5 * (self.frame.width - topViewFrame.height)
        topViewFrame.origin.y = 0.5 * (self.frame.height - totalHeight)
        topView.frame = topViewFrame
       
        bottomViewFrame.origin.x = 0.5 * (self.frame.width - bottomViewFrame.width)
        bottomViewFrame.origin.y = topViewFrame.maxY + self.midSpace
        bottomView.frame = bottomViewFrame;
    }
}
